// pages/commodity_admin/commodity_admin.js
import basesup from '~/utils/supplier.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        Barheight: 48,
        keyword: '',

        leftactiveKey: 0,
        shangpinlist: [],
        newleftlist: false,

        groupresult: [],
        pages: 1,
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },
    naveclassification() {
        wx.navigateTo({
            url: '../../pages/classification/classification',
        })
    },
    companyonClose() {
        this.setData({
            newleftlist: false
        })
    },
    openleftlist() {
        if (this.data.groupresult.length == 0) {
            wx.showToast({
                title: '暂未选择商品',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        this.setData({
            newleftlist: true
        })
    },
    async getshangpin() {
        let resdata = await basesup.storeClass()
        this.setData({
            shangpinlist: resdata.data.Result.records
        })
        this.gatclassiflist()
    },
    naveaddproduct() {
        wx.navigateTo({
            url: '../../pages/addproduct/addproduct',
        })
    },
    leftonChange(items) {
        this.setData({
            leftactiveKey: items.detail,
            groupresult: [],
        })
        this.gatclassiflist()
    },
    async gatclassiflist() {
        let reqdata = {
            "current": this.data.pages,
            "pageSize": 10,
            "menuBar": this.data.shangpinlist[this.data.leftactiveKey].categoryName,
            "keyword": this.data.keyword
        }
        let resdata = await basesup.supplierList(reqdata)
        // console.log(resdata)
        this.setData({
            rightlist: resdata.data.result.Data
        })
    },
    grouponChange(items) {

        this.setData({
            groupresult: items.detail,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getshangpin()
        let res = wx.getSystemInfoSync()
        if (res && res['system']) {
            this.setData({
                Barheight: res.statusBarHeight
            })
        }

    },
    async onConfirmleft(items) {
        console.log(items)
        if (this.data.leftactiveKey == items.detail.index) {
            wx.showToast({
                title: '选择的为同类型',
                icon: 'none',
                duration: 2000
            })
            return false
        }

        let numb = this.data.groupresult.length
        let success = 0
        for (const strs in this.data.groupresult) {
            let reqdaat = {
                "menuBar": items.detail.value.categoryName,
                "proId": this.data.groupresult[strs],
            }
            let resdata = await basesup.allclassifupdata(reqdaat)
            if (resdata.code == 200) {
                success = success + 1
            }
        }
        if (numb == success) {
            wx.showToast({
                title: '修改成功',
                icon: 'success',
                duration: 2000
            })

        }else{
            wx.showToast({
                title: '修改失败',
                icon: 'error',
                duration: 2000
            })
        }
        this.gatclassiflist()
        this.companyonClose()
    },
    endscroll() {
        console.log(("end"))
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getshangpin()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        console.log("1")
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        console.log("3")
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})